package com.icangya.service.impl;

import com.icangya.DAO.*;
import com.icangya.domain.*;
import com.icangya.service.IRedWineGameService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service("RedWineGameServiceImpl")
public class RedWineGameServiceImpl implements IRedWineGameService {


//    @Autowired
//    private IUserFootDAO iUserDAO;
//    @Autowired
//    private IBalanceDAO iBalanceDAO;
//    @Autowired
//    private ICommissionRecordDAO iCommissionRecordDAO;
//    @Autowired
//    private ICashRegisterDAO iCashRegisterDAO;
//    @Autowired
//    private IRechargeRecordDAO iRechargeRecordDAO;
//    @Autowired
//    private ICommissionFootDAO iCommissionFootDAO;
//    @Autowired
//    private IProxyLevelDAO iProxyLevelDAO;
//    @Autowired
//    private IBadUserDAO iBadUserDAO;
//    @Autowired
//    private  IPayBeanDAO iPayBeanDAO;


    @Autowired
    private  IRwshopDAO iRwshopDAO;
    @Autowired
    private  IRwshopPicDAO iRwshopPicDAO;
    @Autowired
    private  IRwGoodOrderDAO iRwGoodOrderDAO;
    @Autowired
    private IRwBuyCarDAO iRwBuyCarDAO;
    @Autowired
    private IRwShopVipDAO iRwShopVipDAO;
    @Autowired
    private ICommissionRecordDAO iCommissionRecordDAO;
    @Autowired
    private ICommissionFootDAO iCommissionFootDAO;
    @Autowired
    private IPayInfoDAO iPayInfoDAO;
    @Autowired
    private IGainRecordDAO iGainRecordDAO;
    @Autowired
    private IRwVailableDAO iRwVailableDAO;
    @Autowired
    private IRwShopSharesRecordDAO iRwShopSharesRecordDAO;
    @Autowired
    private IRwShopSharesDAO iRwShopSharesDAO;
    @Autowired
    private IRwUserAddressDAO iRwUserAddressDAO;
    @Autowired
    private IUserFootDAO iUserFootDAO;
    @Autowired
    private IRwCarousePicDAO iRwCarousePicDAO;
    @Autowired
    private IRwUserBankDAO iRwUserBankDAO;
    @Autowired
    private IRwBankDAO iRwBankDAO;


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 根据字符串查询用户
     * @param lineStr
     * @return
     */
    @Override
    public User selectUserByLineStr(String lineStr) {
        return iUserFootDAO.selectUserByLineStr(lineStr);
    }
    /**
     * 根据手机号码查询用户
     * @param mobilePhone
     * @return
     */
    @Override
    public User selectUserByMobilePhone(String mobilePhone) {
        return iUserFootDAO.selectUserByMobilePhone(mobilePhone);
    }

    /**
     * 根据上架状态（Status）获取一条商品记录，status=1：已上架，status=0：已下架
     * @return
     */
    @Override
    public Rwshop getRwshop(String status) {
        List<Rwshop> rwshopList = iRwshopDAO.list();
        for (int i=0 ;i<rwshopList.size();i++){
            if (status.equals(rwshopList.get(i).getStatus())){
                return rwshopList.get(i);
            }
        }
        return null;
    }


    /**
     * 修改商品上架状态
     * @return
     */
    @Override
    public void setRwshopToState(long shopid,int status){
        Rwshop rwshop = iRwshopDAO.get(shopid);
        rwshop.setStatus(String.valueOf(status));
        iRwshopDAO.update(rwshop);
    }
    /**
     * 修改商品
     * @return
     */
    @Override
    public boolean updateRwshop(Rwshop rwshop){
       return  iRwshopDAO.update(rwshop);
    }


    /**
     * 根据商品id获取商品的图片列表
     * @param id
     * @return
     */
    @Override
    public List<RwshopPic> getRwshopPicListByShopId(long id) {
        return iRwshopPicDAO.getRwShopPicListByShopId(id);
    }


    /**
     * 根据ID获取商品
     * @param id
     * @return
     */
    @Override
    public Rwshop getRwshopById(long id) {
        return iRwshopDAO.get(id);
    }

    /**
     * 根据用户的id获取用户的所有订单信息
     * @param userid
     * @return
     */
    @Override
    public List<RwGoodOrder> getRwGoodOrdersByUserId(long userid) {

        return iRwGoodOrderDAO.getOrdersByUserId(userid);
    }
    /**
     * 所有订单信息
     * @return
     */
    @Override
    public List<RwGoodOrder> getRwGoodOrders() {
        return iRwGoodOrderDAO.list();
    }

    /**
     * 根据支付状态和发货状态所有订单信息
     * @return
     */
    @Override
    public List<RwGoodOrder> getOrderListByPayStataAndSendStatus(int payStatus,int sendStatus){
        return iRwGoodOrderDAO.getOrderListByPayStataAndSendStatus(payStatus,sendStatus);
    }

    /**
     * 添加一条订单信息
     * @param rwGoodOrder
     * @return
     */
    @Override
    public long saveGoodOrder(RwGoodOrder rwGoodOrder) {
        return (long) iRwGoodOrderDAO.save(rwGoodOrder);
    }

    /**
     * 更新订单信息
     * @param rwGoodOrder
     * @return
     */
    @Override
    public boolean updateGoodOrder(RwGoodOrder rwGoodOrder) {
        return iRwGoodOrderDAO.update(rwGoodOrder);
    }


    /**
     * 根据订单id（不是订单号）获取单条订单详细信息
     * @param id
     * @return
     */
    @Override
    public RwGoodOrder getRwGoodOrderById(long id) {
        return iRwGoodOrderDAO.get(id);
    }

    /**
     * 根据订单号获取单条订单详细信息
     * @param orderId
     * @return
     */
    @Override
    public RwGoodOrder getRwGoodOrderByOrderId(String orderId) {
        return iRwGoodOrderDAO.getRwGoodOrderByOrderId(orderId).get(0);
    }

    /**
     * 添加购物车
     * @param rwBuyCar
     * @return
     */
    @Override
    public long saveBuyCar(RwBuyCar rwBuyCar) {
        return (long) iRwBuyCarDAO.save(rwBuyCar);
    }

    /**
     * 获取购物车列表
     * @param userid
     * @return
     */
    @Override
    public List<RwBuyCar> getBuyCarEveryGood(long userid) {
        return iRwBuyCarDAO.getCarEveryGoodsByUserid(userid);
    }

    /**
     * 添加vip用户
     * @param rwShopVip
     * @return
     */
    @Override
    public long saveShopVip(RwShopVip rwShopVip) {
        return (long) iRwShopVipDAO.save(rwShopVip);
    }

    /**
     * 添加返佣记录
     * @param commissionRecord
     * @return
     */
    @Override
    public long saveCommissionRecord(CommissionRecord commissionRecord) {

        return (long) iCommissionRecordDAO.save(commissionRecord);
    }

    @Override
    public Commission getCommissionByUserid(long userid) {
        return iCommissionFootDAO.selectUserCommisByUserid(userid);
    }

    /**
     * 添加支付记录
     * @param fbPayInfo
     * @return
     */
    public long saveFbPayInfo(FbPayInfo fbPayInfo){
        return (long) iPayInfoDAO.save(fbPayInfo);
    }

    /**
     * 根据日期获取支付记录列表
     * @param payDate
     * @return
     */
    @Override
    public List<FbPayInfo> getFbPayInfoListByDate(Timestamp payDate) {
        return iPayInfoDAO.getFbPayInfoListByDate(payDate);
    }

    /**
     * 添加当天总收入记录
     */
    @Override
    public long saveGainRecord(GainRecord gainRecord) {
        return (long) iGainRecordDAO.save(gainRecord);
    }

    /**
     * 获取变量记录
     * @param id
     * @return
     */
    @Override
    public RwVariable getRwVariableById(long id) {
        return iRwVailableDAO.get(id);
    }

    @Override
    public boolean updateRwVariable(RwVariable rwVariable) {
        return iRwVailableDAO.update(rwVariable);
    }

    /**
     * 获取vip用户列表
     * @return
     */
    @Override
    public List<RwShopVip> getVipUser() {
        return iRwShopVipDAO.list();
    }

    /**
     * 根据userid获取vip用户
     */
    @Override
    public List<RwShopVip> getVipUserByuserid(long userid) {
        return iRwShopVipDAO.getVipUserByuserid(userid);
    }

    /**
     * 添加股份记录
     * @param rwShopSharesRecord
     * @return
     */
    @Override
    public long insertRwShopSharesRecord(RwShopSharesRecord rwShopSharesRecord) {
        return (long) iRwShopSharesRecordDAO.save(rwShopSharesRecord);
    }

    /**
     * 添加用户股份权表
     */
    @Override
    public long insertRwShopShares(RwShopShares rwShopShares) {
        return (long) iRwShopSharesDAO.save(rwShopShares);
    }

    /**
     * 更新用户的总股份
     * @param userid
     * @param sharesNum
     * @return
     */
    @Override
    public boolean updateRwShopShares(long userid ,int sharesNum) {
        RwShopShares rwShopShares =  iRwShopSharesDAO.getRwShopSharesByUserId(userid);

        rwShopShares.setSharesNum(rwShopShares.getSharesNum() + sharesNum);

        boolean flag  = iRwShopSharesDAO.update(rwShopShares);

        return flag;

    }
    /**
     * 获取所有用户的总股份
     */
    @Override
    public int getSharesSum() {
        List<RwShopShares> rwShopSharesList = iRwShopSharesDAO.list();
        int zgf = 0;
        for (int i=0 ;i<rwShopSharesList.size();i++){
            zgf = zgf + rwShopSharesList.get(i).getSharesNum();
        }
        return zgf;
    }

    @Override
    public RwShopShares getSharesSum(long userid) {
        return iRwShopSharesDAO.getRwShopSharesByUserId(userid);
    }


    /**
     * 获取占有股份的用户列表
     */
    @Override
    public List<RwShopShares> getHaveSharesUser() {
        return  iRwShopSharesDAO.list();
    }


    /**
     * 添加用户地址
     * @param rwUserAddress
     * @return
     */
    @Override
    public long insertUserAddress(RwUserAddress rwUserAddress) {
        return (long) iRwUserAddressDAO.save(rwUserAddress);
    }

    @Override
    public void deleteUserAddress(long id) {
        RwUserAddress rwUserAddress = iRwUserAddressDAO.get(id);
        iRwUserAddressDAO.remove(rwUserAddress);
    }

    /**
     * 获取用户的地址列表
     * @param userid
     * @return
     */
    @Override
    public List<RwUserAddress> getUserAddressList(long userid) {
        return iRwUserAddressDAO.getListByuserid(userid);
    }


    /**
     * 根据地址ID获取用户的地址
     * @param id
     * @return
     */
    @Override
    public RwUserAddress getUserAddressById(long id) {
        return iRwUserAddressDAO.getUserAddressById(id);
    }

    @Override
    public List<RwCarousePic> getCarouseList() {
        return iRwCarousePicDAO.list();
    }

    @Override
    public List<FbPayInfo> getPayInfolist() {
        return iPayInfoDAO.list();
    }

    @Override
    public long saveShopInfoPic(RwshopPic rwshopPic) {
        return (long) iRwshopPicDAO.save(rwshopPic);
    }

    /**
     * 获取所有申请提现的用户以及银行卡信息
     * @return
     */
    @Override
    public List<RwUserBank> selectAllNeedPayInfo(){
        return  iRwUserBankDAO.selectAllNeedPayInfo();
    }

    @Override
    public long saveUserBank(RwUserBank rwUserBank) {
        return (long) iRwUserBankDAO.save(rwUserBank);
    }

    @Override
    public boolean updateUserBank(RwUserBank rwUserBank) {
        return iRwUserBankDAO.update(rwUserBank);
    }

    @Override
    public RwUserBank getUserBankById(long id) {
        return iRwUserBankDAO.get(id);
    }

    /**
     * 获取userbank  by  userid
     * @param userid
     * @return
     */
    @Override
    public List<RwUserBank> getUserBanksByUserid(long userid){
        return iRwUserBankDAO.getUserBanksByUserid(userid);
    }


    /**
     *添加轮播图记录
     * @param rwCarousePic
     * @return
     */
    @Override
    public long addCarouse(RwCarousePic rwCarousePic){
        return (long) iRwCarousePicDAO.save(rwCarousePic);
    }

    @Override
    public long addUserbankCard(RwBank rwBank) {
        return (long) iRwBankDAO.save(rwBank);
    }

    @Override
    public List<RwBank> getUserbankCardListByuserid(long userid) {
        return iRwBankDAO.getBankByUserid(userid);
    }

    @Override
    public RwBank getUserbankCardBybankid(long id) {
        return iRwBankDAO.getUserbankCardBybankid(id);
    }

    /**
     * 删除轮播图
     * @param id
     */
    @Override
    public void deleteCarouseById(long id) {
        iRwCarousePicDAO.remove(iRwCarousePicDAO.get(id));
    }
}
